<?php 
class Store_Model_Tree extends Zen_Tree {
	
	public function __construct() {
		$table = new Zend_Db_Table ( 'store_tree' );
		$this->table = $table;
		$types = array (
			'root' => array (
				'icon' => '/cms/res/icons/drive.png', 
				'label' => 'Салоны', 
				'valid_children' => array ('city' ), 
				'start_drag' => false, 
				'move_node' => false, 
				'delete_node' => false, 
				'remove' => false ), 
			'city' => array (
				'table' => 'store_city', 
				'form' => 'Store_Form_City', 
				'label' => 'город', 
				'view' => 'form.phtml', 
				'icon' => '/cms/res/icons/folder.png', 
				'valid_children' => array ('salon' ), 
				'start_drag' => true, 
				'move_node' => true, 
				'delete_node' => true, 
				'remove' => true ), 
			'salon' => array (
				'table' => 'store_salon', 
				'form' => 'Store_Form_Salon', 
				'label' => 'салон', 
				'view' => 'form.phtml', 
				'icon' => '/cms/res/icons/cart.png', 
				'valid_children' => 'none', 
				'start_drag' => true, 
				'move_node' => true, 
				'delete_node' => true, 
				'remove' => true ) 
		);
		$this->setTypes ( $types );
	}
	
	public function getCityByAlias($alias) {
		if (! $alias)
			return false;
		$res = $this->select()
					->setDepth(0)
					->setColumns(array('tree'=>array('id','parent_id'),'city'=>array('label', 'shirota','dolgota','sortable','alias','phone','custom','country','sale_text',)))
					->setOrder('tree.left')
					->getOne("tree.type='city' and city.alias='".$alias."'");
		return $res;
	}
	
	public function getCityList($where = '') {
		$where = ($where) ? ' and '.$where : '';
		$res = $this->select()
					->setDepth(0)
					->setColumns(array('tree'=>array('id','parent_id'),'city'=>array('label', 'shirota','dolgota','sortable','alias','phone','country',)))
					->setOrder('tree.left')
					->getData("tree.type='city' $where");
		return $res;
	}
	
	public function getSalonListByAlias($alias) {
		if (! $alias)
			return false;
		$city = $this->getCityByAlias($alias);
		if (! $city )
			return false;
		$res = $this->select()
					->setDepth(0)
					->setParentId($city['id'])
					->setColumns(array('tree'=>array('id','parent_id'),'salon'=>array('label','address', 'hour', 'phones','email','images','type','shirota','dolgota','alias','text','active','in_filter')))
					->setOrder('tree.left')
					->getData("tree.type='salon'");
		return $res;
	}
	
}